package com.trandroid.BubblesMap;

import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.database.Cursor;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.javielinux.BubblesMap.R;
import com.trandroid.BubblesMap.view.MapLocation;
import com.trandroid.BubblesMap.view.MapLocationViewer;

public class BubblesMap extends MapActivity {

	private PicDb mDbHelper;
	private Cursor mNotesCursor;

	private static final int CAMARA=0;
	public static final int TAKE_PIC = Menu.FIRST;

	MapLocationViewer mMapView;
	MapController mMapController;
	LocationManager locationManager;
	
	Double geoLat;
	Double geoLng;	
	
	private final LocationListener locationListener = new LocationListener() {
		public void onLocationChanged(Location location) {
			updateWithNewLocation(location);
		}
		public void onProviderDisabled(String provider){
			updateWithNewLocation(null);
		}
		public void onProviderEnabled(String provider){ }
		public void onStatusChanged(String provider, int status,
				Bundle extras){ 

		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		mDbHelper = new PicDb(this);
		mDbHelper.open();
		


		FrameLayout frame = new FrameLayout(this);

		mMapView = new MapLocationViewer(this, "0G1HVRkLu7NwnnsU9LIMMJoQdbpIwUsGI4fNwnw");

		frame.addView(mMapView);

		setContentView(frame); 	

		mMapController = mMapView.getController();
		mMapView.setTraffic(true);
		mMapView.setSatellite(true);
		mMapController.setZoom(15);

		View zoomControls = mMapView.getZoomControls();

		FrameLayout.LayoutParams p = 
			new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT, Gravity.BOTTOM + Gravity.CENTER_HORIZONTAL);

		frame.addView(zoomControls, p);
		
		
		String context = Context.LOCATION_SERVICE;
		locationManager = (LocationManager)getSystemService(context);
		String provider = LocationManager.GPS_PROVIDER;
		Location location = locationManager.getLastKnownLocation(provider);

		locationManager.requestLocationUpdates(provider, 0, 0,locationListener);
		updateWithNewLocation(location);
		

		//GeoPoint point = new GeoPoint((int)(36.506004*1E6), (int)(-6.276686*1E6));
		//mMapController.setCenter(point);
		fillData();
	}

	private void fillData() {
		// Get all of the rows from the database and create the item list
		mNotesCursor = mDbHelper.fetchAllPic();

		for (int i=0;i<mNotesCursor.getCount();i++){
			try{
				mNotesCursor.moveToPosition(i);
				int NomColum = mNotesCursor.getColumnIndex(PicDb.KEY_PicName);
				int LatColum = mNotesCursor.getColumnIndex(PicDb.KEY_PicLati);
				int LonColum = mNotesCursor.getColumnIndex(PicDb.KEY_PicLongi);
				String latitude = mNotesCursor.getString(LatColum);
				String longitude = mNotesCursor.getString(LonColum);
				int patata = Integer.parseInt(latitude);
				int potato = Integer.parseInt(longitude);
				
				GeoPoint point = new GeoPoint(patata,potato);
				mMapView.getManager().addMapLocation(new MapLocation(mMapView, mNotesCursor.getString(NomColum), point, MapLocation.TYPE_BUBBLE));
			}
			catch (Exception e){
				e.printStackTrace();
				break;
			}
		}
	}


	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu); 
		menu.add(0, TAKE_PIC, 0, R.string.take_pic);
		return result;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case TAKE_PIC:
			tomar_foto();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}


	private void tomar_foto() {

		Intent i = new Intent(this, MyCamera.class);
		startActivityForResult(i,CAMARA );

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		Bundle extras = intent.getExtras();
		switch(requestCode) {
		case CAMARA:
			String title = extras.getString("idImg");
			int picLongi = geoLng.intValue();
			int picLati = geoLat.intValue();
			mDbHelper.createBubble(title,String.valueOf(picLati), String.valueOf(picLongi)); 
			
			GeoPoint point = new GeoPoint(picLati,picLongi);
			
			//mDbHelper.createBubble(title, String.valueOf(punto.getLongitudeE6()),String.valueOf(punto.getLatitudeE6()));   
  
			mMapView.getManager().addMapLocation(new MapLocation(mMapView, title, point, MapLocation.TYPE_BUBBLE));
			break;

		}

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	private void updateWithNewLocation(Location location) {
		String latLongString;
		String addressString = "No address found";

		if (location != null) {
			// Update my location marker
			// Update the map location.
			geoLat = location.getLatitude()*1E6;
			geoLng = location.getLongitude()*1E6;
			GeoPoint punto = new GeoPoint(geoLat.intValue(),
					geoLng.intValue());
			mMapController.animateTo(punto);

			double lat = location.getLatitude();
			double lng = location.getLongitude();
			latLongString = "Lat:" + lat + "\nLong:" + lng;
		} else {
			latLongString = "No location found";
		}
		//myLocationText.setText("Your Current Position is:\n" +
				//latLongString + "\n" + addressString);
	}

}